<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item prop="date">
					<el-date-picker
						v-model="filters.date"
						range-separator="至"
						type="datetimerange"
						placeholder="选择时间范围">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTypeList">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<!--<el-input prop="balance" type="hidden"></el-input>
			<el-input prop="points" type="hidden"></el-input>
			<el-input prop="canWithdrawBalance" type="hidden"></el-input>
			<el-input prop="rebateBalance" type="hidden"></el-input>-->
			<el-table-column type="index" label="序号" align="center" width="65">
			</el-table-column>
			<el-table-column prop="name" label="分类名称" align="center">
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" align="center" min-with="140" :formatter="formatDateTime">
			</el-table-column>
			<el-table-column prop="lastEditTime" label="最后编辑时间" align="center" :formatter="formatDateTime">
			</el-table-column>
			<el-table-column prop="lastEditAdmin" label="最后操作人" align="center">
			</el-table-column>
			<el-table-column label="操作" align="center" min-width="160">
				<template slot-scope="scope">
					<el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<div class="toolbar" style="display: flex;justify-content: flex-end;">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="currentPage"
				:page-sizes="[10, 20, 30, 40]"
				:page-size="size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total">
			</el-pagination>
		</div>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="分类名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off" placeholder="不超过8个字" :maxlength="8"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="分类名称" prop="name">
					<el-input v-model="addForm.name" auto-complete="off" placeholder="不超过8个字" :maxlength="8"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { shopTypeList, addShopType, updateShopType, deleteShopType} from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: '',
					date: ''
				},
				start: '',
				end: '',
				pickerBeginDateBefore:{
		            disabledDate: (time) => {
		                let beginDateVal = this.filters.endTime;
		                if (beginDateVal) {
		                    return time.getTime() > beginDateVal;
		                }
		            }
		        },
		        pickerBeginDateAfter:{
		            disabledDate: (time) => {
		                let beginDateVal = this.filters.startTime;
		                if (beginDateVal) {
		                    return time.getTime() < beginDateVal;
		                }
		            }
		        },
				users: [
				// {
				// 	id:'1',
				// 	name: "分类名称测试",
				// 	createTime: "创建时间测试",
				// 	lastEditTime: '编辑时间测试',
				// 	lastEditAdmin: '操作人测试',
				// }
				],
				total: 0,
				currentPage: 1,
				size: 10,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入分类名称', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id:'',
					name: ""
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入分类名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					name: ""
				}

			}
		},
		methods: {
			getTime() { // 时间处理
				let starttime,endtime = ''
				let time = this.filters.date;
				if(!time[0] == '') {
					starttime = this.moment(time[0]).format('YYYY-MM-DD HH:mm:ss')
				}
				if(!time[1] == '') {
					endtime = this.moment(time[1]).format('YYYY-MM-DD HH:mm:ss');
				}
				if(starttime == "Invalid date"){
					starttime="";
				};
				if(endtime == "Invalid date"){
					endtime="";
				};
				this.start = starttime
				this.end = endtime
			},
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			formatDateTime: function (row, column){
				var date = row[column.property];
				if (date == undefined) {
		            return "";
		        }
				return this.moment(date).format("YYYY-MM-DD HH:mm:ss");
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getTypeList();
			},
			handleSizeChange(val) {
				this.size = val
				this.getTypeList();
			},
			//获取角色类型列表
			getTypeList() {
				this.getTime();
				let para = {
					pageIndex: this.page,
					pageSize: this.num,
				};
				if(!this.start == '') {
					para.startTime = this.start
				}
				if(!this.end == '') {
					para.endTime = this.end
				}
				this.listLoading = true;
				shopTypeList(para).then((res) => {
					this.users = res.data.data;
					this.total = res.data.totalCount;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					deleteShopType(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						
						if(res.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getTypeList();
						} else {
							this.$message({
								message: res.message,
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = {
								id: this.editForm.id,
								name: this.editForm.name
							};
							updateShopType(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								if(res.code == 0) {
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getTypeList();
								} else {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
							addShopType(para).then((res) => {
								this.addLoading = false;
								if(res.code == 0) {
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getTypeList();
								} else {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
						});
					}
				});
			}
		},
		mounted() {
			this.getTypeList();
		}
	}

</script>

<style scoped>

</style>
